//
//  AOStatusOriginalView.swift
//  weibo_zha
//
//  Created by _zha on 16/10/28.
//  Copyright © 2016年 渣. All rights reserved.
//

import UIKit
///原创微博
class AOStatusOriginalView: UIView {
    
    // 记录原创微博的底部约束
    var originalViewBottomeCons: Constraint?
    
    // 定义一个属性 ViewModel 供外界赋值
    var statusViewModel: AOStatusViewModel?{
        didSet{
            // 当前的控件赋值了
            // 头像
            headImageView.ao_setImage(withUrlString: statusViewModel?.statusModel?.user?.profile_image_url, placeholderImgName: "avatar_default_big")
            // 昵称
            nameLabel.text = statusViewModel?.statusModel?.user?.name
            // 等级
            membershipImageView.image = statusViewModel?.mbrankImage
            // 认证
            avatarImageView.image = statusViewModel?.verifiedImage
            // 微博正文
            contentLabel.text = statusViewModel?.statusModel?.text
            
            sourceLabel.text = statusViewModel?.sourceStr
//            print(statusViewModel?.statusModel?.created_at)
            timeLabel.text = statusViewModel?.timeStr
            
            //配图赋值
            // 卸载约束
            originalViewBottomeCons?.uninstall()
            // 有配图
            if (statusViewModel?.statusModel?.pic_urls?.count)! > 0 {
                pictureView.picUrls = statusViewModel?.statusModel?.pic_urls
                pictureView.isHidden = false
                self.snp_makeConstraints { (make) in
                    self.originalViewBottomeCons = make.bottom.equalTo(pictureView).offset(10).constraint
                }
                
            }else {
                // 没有配图
                pictureView.isHidden = true
                self.snp_makeConstraints { (make) in
                    self.originalViewBottomeCons = make.bottom.equalTo(contentLabel).offset(10).constraint
                }
            }
        }
    }
   
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  
    private func setupUI() {
        backgroundColor = UIColor.white
        //添加控件
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(membershipImageView)
        addSubview(avatarImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        //添加约束
        headImageView.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 35, height: 35))
            make.top.left.equalTo(10)
        }
        
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headImageView)
            make.left.equalTo(headImageView.snp_right).offset(10)
        }
        
        timeLabel.snp_makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        
        sourceLabel.snp_makeConstraints { (make) in
            make.bottom.equalTo(timeLabel)
            make.left.equalTo(timeLabel.snp_right).offset(10)
        }
        
        membershipImageView.snp_makeConstraints { (make) in
            make.centerY.equalTo(nameLabel)
            make.left.equalTo(nameLabel.snp_right).offset(10)
        }
        
        avatarImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(headImageView.snp_right)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.left.equalTo(headImageView)
//            make.bottom.equalTo(self).offset(-10)
        }
        pictureView.snp_makeConstraints { (make) in
//            make.size.equalTo(CGSize(width: 100, height: 100))
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.left.equalTo(contentLabel)
        }
        
        self.snp_makeConstraints { (make) in
            self.originalViewBottomeCons = make.bottom.equalTo(pictureView).offset(10).constraint
        }

    }
    //懒加载控件
    //头像
    private lazy var headImageView: UIImageView = UIImageView(imgName:"avatar_default_big")
    //昵称
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.black, fontSize: AONormalFontSize, text: "黄鹤")
    //时间
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.blue, fontSize: AOSmallFontSize, text: "现在")
    //来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.blue, fontSize: AOSmallFontSize, text: "江南皮革厂")
    //微博等级
    private lazy var membershipImageView: UIImageView = UIImageView(imgName: "common_icon_membership")
    //认证信息
    private lazy var avatarImageView: UIImageView = UIImageView(imgName: "avatar_vgirl")
    //微博正文
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.black, fontSize: AONormalFontSize, text: "", maxWidth: AOScreeW - 20)
    //配图
    private lazy var pictureView: AOStatusPictrueView = {
        let view =  AOStatusPictrueView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
}
